package cn.mazexal.examapi.common.models;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author: Mazexal
 * Date: 2025/5/11
 */

public class MultiResponse<T> extends Response {
    private static final long serialVersionUID = 1L;
    private Collection<T> data;

    public MultiResponse() {
    }

    public List<T> getData() {
        if (null == this.data) {
            return Collections.emptyList();
        } else {
            return (List)(this.data instanceof List ? (List)this.data : new ArrayList(this.data));
        }
    }

    public void setData(Collection<T> data) {
        this.data = data;
    }

    public static <T> MultiResponse<T> success() {
        MultiResponse<T> response = new MultiResponse();
        response.setSuccess(true);
        return response;
    }

    public static <T> MultiResponse<T> error(String code, String message) {
        MultiResponse<T> response = new MultiResponse();
        response.setSuccess(false);
        response.setErrCode(code);
        response.setErrMessage(message);
        return response;
    }

    public static <T> MultiResponse<T> error(ResponseError error) {
        return error(error.getCode(), error.getMessage());
    }

    public static <T> MultiResponse<T> of(Collection<T> data) {
        MultiResponse<T> response = new MultiResponse();
        response.setSuccess(true);
        response.setData(data);
        return response;
    }
}
